<template>
  <div class="user-form-container">
    <el-form ref="userForm" :model="userForm" label-width="100px" class="user-form">
      <el-form-item label="用户名" class="form-item">
        <el-input v-model="userForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" class="form-item">
        <el-input type="password" v-model="userForm.password"></el-input>
      </el-form-item>
      <el-form-item label="姓名" class="form-item">
        <el-input v-model="userForm.name"></el-input>
      </el-form-item>
      <el-form-item label="性别" class="form-item">
        <el-radio-group v-model="userForm.gender">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="出生日期" class="form-item">
        <el-date-picker v-model="userForm.birthDate" type="date" placeholder="选择日期"></el-date-picker>
      </el-form-item>
      <el-form-item class="form-item">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      userForm: {
        username: '',
        password: '',
        name: '',
        gender: '',
        birthDate: '',
      },
    };
  },
  methods: {
    submitForm() {
      axios.post('/api/user/add', this.userForm)
        .then(response => {
          this.$message.success('用户添加成功');
        })
        .catch(error => {
          this.$message.error('用户添加失败');
        });
    },
    resetForm() {
      this.$refs.userForm.resetFields();
    },
  },
};
</script>

<style scoped>
.user-form-container {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  height: 100vh;
  padding-top: 50px;
  /* 上方间距为 50px */
}

.user-form {
  width: 400px;
}

.form-item {
  margin-bottom: 20px;
}

.el-button {
  margin-right: 10px;
}
</style>
